package network.盗版qq.qqclient.service;

import network.盗版qq.comment.Message;
import network.盗版qq.comment.MessageType;

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.Date;

/**
 * @author mubei
 * @date 2021/12/7
 * @description  该类 提供和消息相关的服务方法
 */
public class SendMessageService {


    /*
    * content   传的内容
    * sender   发送者id
    * getter   接收者id
    * */
    public void sendMessageToOne(String content, String sender,String getter) {

        Message message = new Message();
        message.setContent(content);
        message.setSender(sender);
        message.setGetter(getter);
        message.setSenTime(new Date().toString());
        message.setMesType(MessageType.COMM_MES);
        System.out.println(sender+" 对 "+getter+" 说 "+content);

        try {
            ObjectOutputStream oos =   new ObjectOutputStream(
                    ManageClientConnectServer.getClientConnectServerThread(sender).getSocket()
                            .getOutputStream());
            oos.writeObject(message);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }


    public void sendMessageToAll(String content, String sender) {

        Message message = new Message();
        message.setContent(content);
        message.setSender(sender);
        message.setSenTime(new Date().toString());
        message.setMesType(MessageType.COMM_MES_TO_ALL);
        System.out.println(sender+" 对大家说 "+content);

        try {
            ObjectOutputStream oos =   new ObjectOutputStream(
                    ManageClientConnectServer.getClientConnectServerThread(sender).getSocket()
                            .getOutputStream());
            oos.writeObject(message);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}
